package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.MchntAcctInfoMapper;
import cc.rengu.igas.bomp.common.entity.MchntAcctInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by 王成 on 2020/3/2
 */
public class MchntAcctInfoMapperImpl implements MchntAcctInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public int insertMchntAcctInfo(MchntAcctInfo mchntAcctInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertBompMchntAcctInfo");
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, mchntAcctInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记商户结算账户信息失败,instId:<{}>,mchntNo:<{}>,iReturnCode:<{}>", mchntAcctInfo.getInstId(), mchntAcctInfo.getMchntNo(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateMchntAcctInfoById(MchntAcctInfo mchntAcctInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateBompMchntAcctInfoById");
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, mchntAcctInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记商户结算账户信息失败,instId:<{}>,mchntNo:<{}>,id:<{}>,iReturnCode:<{}>", mchntAcctInfo.getInstId(), mchntAcctInfo.getMchntNo(), mchntAcctInfo.getId(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public MchntAcctInfo selectMchntAcctInfoById(String instId, String id) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompMchntAcctInfoById");
        MchntAcctInfo mchntAcctInfo = new MchntAcctInfo();
        mchntAcctInfo.setInstId(instId);
        mchntAcctInfo.setId(id);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntAcctInfo, mchntAcctInfo);
        if (0 != iReturnCode) {
            rglog.error("获取商户结算账户信息失败,instId:<{}>,id:<{}>,iReturnCode:<{}>", instId, id, iReturnCode);
            return null;
        }
        return mchntAcctInfo;
    }

    @Override
    public MchntAcctInfo selectMchntAcctInfoByPrimaryKey(String instId, String mchntNo, String baseProdCode, String fundType) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompMchntAcctInfoByPrimaryKey");
        MchntAcctInfo mchntAcctInfo = new MchntAcctInfo();
        mchntAcctInfo.setInstId(instId);
        mchntAcctInfo.setMchntNo(mchntNo);
        mchntAcctInfo.setBaseProdCode(baseProdCode);
        mchntAcctInfo.setFundType(fundType);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntAcctInfo, mchntAcctInfo);
        if (0 != iReturnCode) {
            rglog.error("获取商户结算账户信息失败,instId:<{}>,mchntNo:<{}>,baseProdCode:<{}>,fundType:<{}>,iReturnCode:<{}>", instId, mchntNo, baseProdCode, fundType, iReturnCode);
            return null;
        }
        return mchntAcctInfo;
    }

    @Override
    public List<MchntAcctInfo> selectMchntAcctInfoByMchntNo(String instId, String mchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompMchntAcctInfoByMchntNo");
        MchntAcctInfo mchntAcctInfo = new MchntAcctInfo();
        mchntAcctInfo.setInstId(instId);
        mchntAcctInfo.setMchntNo(mchntNo);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntAcctInfo, MchntAcctInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取商户结算账户信息失败,instId:<{}>,mchntNo:<{}>", instId, mchntNo);
            return null;
        }
        return objectList.stream().map(item -> (MchntAcctInfo) item).collect(Collectors.toList());
    }

    @Override
    public int updateAcctSignFlagById(MchntAcctInfo mchntAcctInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateBompAcctSignFlagById");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntAcctInfo, null);
        if (0 != iReturnCode) {
            rglog.error("更新商户结算账户信息签约标识失败,instId:<{}>,mchntNo:<{}>,id:<{}>,iReturnCode:<{}>", mchntAcctInfo.getInstId(), mchntAcctInfo.getMchntNo(), mchntAcctInfo.getId(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public List<MchntAcctInfo> selectMchntAcctInfoByAcctNo(String instId, String acctNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompMchntAcctInfoByAcctNo");
        MchntAcctInfo mchntAcctInfo = new MchntAcctInfo();
        mchntAcctInfo.setInstId(instId);
        mchntAcctInfo.setSettleAcctNo(acctNo);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntAcctInfo, MchntAcctInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取商户结算账户信息失败,instId:<{}>,acctNo:<{}>", instId, acctNo);
            return null;
        }
        return objectList.stream().map(item -> (MchntAcctInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<MchntAcctInfo> selectMchntAcctInfoByMchntNoAndAcctNo(String instId, String mchntNo, String acctNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompMchntAcctInfoByMchntNoAndAcctNo");
        MchntAcctInfo mchntAcctInfo = new MchntAcctInfo();
        mchntAcctInfo.setInstId(instId);
        mchntAcctInfo.setMchntNo(mchntNo);
        mchntAcctInfo.setSettleAcctNo(acctNo);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntAcctInfo, MchntAcctInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取商户结算账户信息失败,instId:<{}>,acctNo:<{}>", instId, acctNo);
            return null;
        }
        return objectList.stream().map(item -> (MchntAcctInfo) item).collect(Collectors.toList());
    }
}
